package com.zhuss.timer;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CareOrderBo;
import com.zhuss.bean.bo.order.order.CareOrderRefundBo;
import com.zhuss.bean.vo.order.order.CareOrderRefundVo;
import com.zhuss.bean.vo.order.order.CareOrderVo;
import com.zhuss.common.Processor;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderRefundDao;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.ListBean;

@Component
@Configuration      
@EnableScheduling
public class RefreshOrderStatusScheduleTask {
	
	 private static final org.slf4j.Logger log = LoggerFactory.getLogger(RefreshOrderStatusScheduleTask.class);
	
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderRefundDao orderRefundDao;
	
	
	@Autowired
	@Qualifier("careOrderForUpdate")
	private Processor<CareOrderVo, CareOrderBo> careOrderForUpdate;
	
	@Autowired
	@Qualifier("careOrderRefundForUpdate")
	private Processor<CareOrderRefundVo, CareOrderRefundBo> careOrderRefundForUpdate;
	
	
    @Scheduled(cron = "*/105 * * * * ?")
    private void configureTasks() {
    	Date today = new Date();
    	List<Long> orderIdList = orderDao.listOrderNoTop100(
    			new ListBean<Integer>()
    			.build(OrderStatus.未支付.key())
    			.build(OrderStatus.已提交支付.key())
    			.build(OrderStatus.用户支付中.key())
    			.getList(), DateUtil.toDateOnly(today));
    	
    	log.error("当前线程: " + Thread.currentThread().getName());
    	CareOrderBo bo = new CareOrderBo();
    	bo.setConcurrentSystem(10);
    	bo.setConcurrentUserId(0l);
    	for (Long orderId : orderIdList) {
    		log.error("执行静态定时任务时间: " + LocalDateTime.now() + " orderId -----" + orderId);
    		bo.setOrderId(orderId);
    		CareOrderVo result = null;
    		try {
				result = careOrderForUpdate.process(bo);
			} catch (DzcxException e) {
				e.printStackTrace();
			}
    		
    		log.info(JsonUtil.toJson(result));
    		System.err.println("执行静态定时任务时间: " + LocalDateTime.now() + " orderId -----" + orderId);
    		System.err.println("当前线程: " + Thread.currentThread().getName());
    		System.out.println(JsonUtil.toJson(result));
    	}
    	
    	List<String> orderRefundNoList = orderRefundDao.listOrderRefundNoTop100(
    			new ListBean<Integer>()
    			.build(OrderStatus.已提交退款申请.key())
    			.build(OrderStatus.退款处理中.key())
    			.getList(), DateUtil.toDateOnly(today));
    	
    	log.error("当前线程: " + Thread.currentThread().getName());
    	CareOrderRefundBo bo2 = new CareOrderRefundBo();
    	bo2.setConcurrentSystem(10);
    	bo2.setConcurrentUserId(0l);
    	for (String orderRefundNo : orderRefundNoList) {
    		log.error("执行静态定时任务时间: " + LocalDateTime.now() + " orderRefundNo -----" + orderRefundNo);
    		bo2.setOrderRefundNo(orderRefundNo);
    		CareOrderRefundVo result = null;
    		try {
				result = careOrderRefundForUpdate.process(bo2);
			} catch (DzcxException e) {
				e.printStackTrace();
			}
    		
    		log.info(JsonUtil.toJson(result));
    		System.err.println("执行静态定时任务时间: " + LocalDateTime.now() + " orderRefundNo -----" + orderRefundNo);
    		System.err.println("当前线程: " + Thread.currentThread().getName());
    		System.out.println(JsonUtil.toJson(result));
    		
    	}
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }
	
}
